from invidious import Invidious
import requests
import sys

from plugins.plugin import Plugin

sys.path.append("..")
from src.video import Video
from src.history import History
from src.config import Config

class InvidiousPopular(Plugin):

    def __init__(self, history: History=History(), config: Config=Config()) -> None:
        super().__init__(history, config)

        self.history = history
        self.config = config

        self.name = "[+] Invidious: Popular"
        self.category = "Invidious"

        self.params = {}

        self.flag = "-ip"
        self.full_flag = "--ivpopular"
        self.flag_help = "show popular videos from invidious"
        self.flag_action = "store_true"

    def get_popular(self):
        popular = self.invidious.get_popular()

        items = []
        for video in popular:
            if video == None: continue
            video.convert(Video)
            video.url = self.invidious.get_working_mirror() + "/watch?v=" + video.videoId
            video.isWatched = video.in_history(self.history)
            video = video.to_json()
            items.append(video)

        return items

    def get_items(self, params):
        return self.get_popular()



